package com.fjrz.bbxwj.main.host

import androidx.lifecycle.Observer
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.module.lemlin.base.BaseAbstractFragment
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.ChatIndex
import com.fjrz.bbxwj.entity.MessageInfo
import com.fjrz.bbxwj.entity.SocketType
import com.fjrz.bbxwj.main.notice.MessageBulletinActivity
import com.fjrz.bbxwj.main.notice.MessageChatActivity
import com.fjrz.bbxwj.main.notice.MessageMutualActivity
import com.fjrz.bbxwj.main.notice.MessageSystemActivity
import com.fjrz.bbxwj.main.notice.adapter.MessageChatAdapter
import com.fjrz.bbxwj.mode.vm.MessageViewModel
import com.fjrz.bbxwj.util.HttpWebSocketUtils
import com.module.lemlin.mode.ViewModeFragment
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.view.LoginAuthPopup
import kotlinx.android.synthetic.main.fragment_main_msg.*

class MainMsgFragment : ViewModeFragment<MessageViewModel>() {


    companion object {
        fun newInstance(): MainMsgFragment {
            return MainMsgFragment()
        }
    }

    private val mMessageChatAdapter = MessageChatAdapter()

    override fun viewModel(): MessageViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_msg

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(frameMessageBar)


        rvMessageChatBody.adapter = mMessageChatAdapter

        mMessageChatAdapter.setOnItemClickListener { adapter, view, position ->
            val itemData: ChatIndex = mMessageChatAdapter.getItem(position)
            MessageChatActivity.open(
                activity = requireActivity(),
                title = itemData.object_user.nickname,
                objectUserId = itemData.object_user.id
            )
        }

        //官方公告
        clMessageAnnouncement.setOnClickListener {
            MessageBulletinActivity.open(requireActivity())
        }
        //系统消息
        clMessageSystem.setOnClickListener {
            MessageSystemActivity.open(requireActivity())
        }
        //互动消息
        clMessageInteractive.setOnClickListener {
            MessageMutualActivity.open(requireActivity())
        }

    }

    override fun onResume() {
        super.onResume()
        viewModel.messageInfo()
        viewModel.messageChatIndex()
    }

    override fun initViewMode() {
        viewModel.messageInfoLiveData.observe(viewLifecycleOwner, Observer { response ->
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.let {
                fillData(it)
            }
        })

        viewModel.chatIndexLiveData.observe(viewLifecycleOwner, Observer { response ->
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.let {
                mMessageChatAdapter.setNewInstance(it)
            }
        })
    }

    override fun onEventMainThread(any: Any) {
        if (any is LoginAuthPopup.Action) {
            if (any != LoginAuthPopup.Action.ACTION_LOGIN_SUCCESS)
                return
            viewModel.messageInfo()
            viewModel.messageChatIndex()
        } else if (any is SocketType) {
            viewModel.messageChatIndex()
        }
    }

    private fun fillData(messageInfo: MessageInfo) {

        val announcementContent2: String = try {
            val announcementContent0: String = messageInfo.last_announcement?.content
                ?: throw Exception()
            val announcementContent1: String =
                announcementContent0.substring(3, announcementContent0.indexOf("</p>"))
            announcementContent1.removeRange(
                announcementContent1.indexOf("<"),
                announcementContent1.indexOf(">") + 1
            )
        } catch (e: Exception) {
            "暂无最新公告"
        }

        val systemContent2: String = try {
            val systemContent0: String = messageInfo.last_message?.content
                ?: throw Exception()
            val systemContent1: String = systemContent0.substring(3, systemContent0.indexOf("</p>"))
            systemContent1.removeRange(systemContent1.indexOf("<"), systemContent1.indexOf(">") + 1)
        } catch (e: Exception) {
            "暂无最新消息"
        }

        tvMessageAnnouncementContent.text = announcementContent2
        tvMessageAnnouncementTime.text = if (messageInfo.last_announcement == null) "" else
            TimeUtils.millis2String(messageInfo.last_announcement.createtime * 1000, "MM-dd HH:mm")
        tvMessageSystemContent.text = systemContent2
        tvMessageSystemTime.text = if (messageInfo.last_message == null) "" else
            TimeUtils.millis2String(messageInfo.last_message.createtime * 1000, "MM-dd HH:mm")

    }

}